<!-- 校园编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改校园':'添加校园'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item label="所属学校:" prop="school_id">
        <el-select
          filterable
          clearable
          v-model="form.school_id"
          @change="getCampusTypeList"
          size="small"
          placeholder="-请选择学校-"
          class="ele-block">
          <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      
       <el-form-item label="校园类型:" prop="campus_type">
        <el-select
          filterable
          clearable
          v-model="form.campus_type"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择校园类型-"
          class="ele-block">
          <el-option v-for="item in campusTypeList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item
        label="通知标题:"
        prop="title">
        <el-input
          :maxlength="20"
          v-model="form.title"
          placeholder="请输入通知标题"
          clearable/>
      </el-form-item>
      
      <el-form-item label="通知内容:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          v-model="form.content"
          placeholder="请输入通知内容"/>
      </el-form-item>
      <el-form-item label="附件：">
        <uploadImage :limit="1" v-model="form.imgs"></uploadImage>
         <el-link :underline="false" type="danger">注:图片格式为jpg、png</el-link>
      </el-form-item>

      <el-form-item label="全校发布:">
        <el-radio-group
          :disabled="disabled" 
          @input="getDeviceList"
          v-model="form.is_all">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="2">否</el-radio>
                </el-radio-group>
      </el-form-item>
      
      <el-form-item
        v-if="form.is_all==2"
        label="接收设备:"
        prop="devices">
        <el-select
          filterable
          clearable
          multiple
          v-model="form.devices"
          size="small"
          placeholder="-请选择接收设备-"
          class="ele-block">
          <el-option v-for="item in deviceList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="是否置顶:">
        <el-radio-group
          v-model="form.is_top">
                  <el-radio :label="1">已置顶</el-radio>
                  <el-radio :label="2">未置顶</el-radio>
                </el-radio-group>
      </el-form-item>

      <el-form-item label="发布状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">立即发布</el-radio>
                  <el-radio :label="2">定时发布</el-radio>
                </el-radio-group>
      </el-form-item>
      
      <el-form-item
        v-if="form.status==2"
        label="发布时间:"
        prop="release_time">
          <el-date-picker
              type="datetime"
              v-model="form.release_time"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请输入发布时间">
         </el-date-picker>
      </el-form-item>

      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'CampusEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              school_id: [
          {required: true, message: '请选择所属学校', trigger: 'blur'}
        ],
                  campus_type: [
          {required: true, message: '请选择校园类型', trigger: 'blur'}
        ],
                  title: [
          {required: true, message: '请输入通知标题', trigger: 'blur'}
        ],

        is_all: [
          {required: true, message: '请选择全校发布', trigger: 'blur'}
        ],

        is_top: [
          {required: true, message: '请选择是否置顶', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择发布状态', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入通知内容', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学校列表
      schoolList: [],
      // 校园类型列表
      campusTypeList: [],
      // 设备列表
      deviceList: [],
      disabled: false
    };
  },
  mounted() {
    this.getSchoolList(); // 查询学校列表
    this.getCampusTypeList();
  },
  created() {
      this.userType();
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
     userType(){
          this.$http.get('/campus/permission').then(res => {
              if(this.isUpdate == 0){
                   if(res.data.data.is_teach == 1){
                     this.form.is_all = 2;
                     this.disabled = true;
                }
              }
              
          }).catch(e => {
            this.$message.error(e.message);
          });
     },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/campus/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取学校列表
     */
    getSchoolList() {
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取校园类型列表
     */
    getCampusTypeList() {
      this.form.campus_type = null;
      this.getDeviceList();
      this.$http.post('/campustype/getCampusTypeList',{school_id: this.form.school_id}).then(res => {
        if (res.data.code === 0) {
          this.campusTypeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取设备列表
     */
    getDeviceList() {
      this.$http.post('/equip/getList',{organization: this.form.school_id, limit: 5000}).then(res => {
        if (res.data.code === 0) {
          this.deviceList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
      
    },
  }
}
</script>

<style scoped>
</style>
